<?php

/**
 * ECSHOP 专题前台
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @author:     webboy <laupeng@163.com>
 * @version:    v2.1
 * ---------------------------------------------
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

if (empty($_REQUEST['act']))
{
	$_REQUEST['act'] = 'index';
}


if ($_REQUEST['act'] == 'index')
{
	$templates = 'ceping_index.dwt';
	$position = assign_ur_here();
	
	$smarty->assign('exam_list',get_exam_list());
	$smarty->assign('page_title',       $position['title']);    // 页面标题
	$smarty->assign('keywords','身体健康测评');
	$smarty->assign('description','利伟保健身体健康测评专题，让你了解自己的健康状况。');
	assign_template();
	$smarty->assign('cfg', $_CFG);	
}
elseif ($_REQUEST['act'] == 'question')
{

	
	$templates = 'ceping_question.dwt';
	$exam_id = isset($_REQUEST['id'])?intval($_REQUEST['id']):0; 
	$exam = get_exam_info($exam_id);
	if (!$exam){
		/* 如果没有找到任何记录则跳回到首页 */
		ecs_header("Location: ./\n");
		exit;
	}
	
	$smarty->assign('question_list',get_question_list($exam_id));
	$smarty->assign('exam',$exam);
	/* 模板赋值 */
	assign_template();
	$position = assign_ur_here(0,$exam['title']);
	$smarty->assign('page_title',       $position['title']);       // 页面标题
	$smarty->assign('show_marketprice', $_CFG['show_marketprice']);
	$smarty->assign('cfg', $_CFG);
	
	
}
elseif ($_REQUEST['act'] == 'result')
{

	$templates = 'ceping_result.dwt';
	$exam_id = isset($_REQUEST['id'])?intval($_REQUEST['id']):0;
	if (!is_array($_POST['ques'])) show_message('页面过期', '', '', 'error');
	$point = 0;
	foreach ($_POST['ques'] as $k=>$v){
		$point += intval($v);
	}
	
	$result = get_result($exam_id,$point);
// 	print_r($result);
	if (empty($result)) exit();
	$smarty->assign('result',$result);
	//模板赋值
	$smarty->assign('cfg', $_CFG);
	assign_template();
	$position = assign_ur_here(0, $result['title']);
	$smarty->assign('page_title', $position['title']);    // 页面标题
	
// 	$db->query('UPDATE '.$ecs->table('exam').' SET join_num=join_num+1 WHERE exam_id='.$exam_id.' LIMIT 1');
}

/* 显示模板 */
$smarty->display($templates);

function get_exam_list()
{
	$sql = 'SELECT * FROM '.$GLOBALS['ecs']->table('exam').' ORDER BY ordering DESC,exam_id';
	$result = $GLOBALS['db']->getAll($sql);
	foreach ($result as $k=>$row){
		$result[$k]['url'] = build_uri('ceping', array('eid'=>$row['exam_id']));
	}
	return $result;
}

function get_exam_info($exam_id = 0)
{
	$sql = 'SELECT e.*,COUNT(eq.question_id) AS ques_count FROM '.$GLOBALS['ecs']->table('exam').' AS e '.
			'LEFT JOIN '.$GLOBALS['ecs']->table('exam_question').' AS eq ON e.exam_id=eq.exam_id '.
			'WHERE e.exam_id='.$exam_id;
	$res = $GLOBALS['db']->getRow($sql);
	return $res;
}

function get_question_list($exam_id = 0)
{
	$sql = 'SELECT eq.* FROM '.$GLOBALS['ecs']->table('exam_question').' AS eq '.
			'WHERE eq.exam_id='.$exam_id;
	$res = $GLOBALS['db']->getAll($sql);
	return $res;
}

function get_result($exam_id = 0,$point = 0)
{
	$sql = 'SELECT er.*,e.title FROM '.$GLOBALS['ecs']->table('exam_result').' AS er '.
			'JOIN '.$GLOBALS['ecs']->table('exam').' AS e ON er.exam_id=e.exam_id '.
			'WHERE er.exam_id='.$exam_id.' AND ((er.point_min<'.$point.' AND er.point_max>'.$point.') OR er.point_min='.$point.' OR er.point_max='.$point.') LIMIT 1';
	$result = $GLOBALS['db']->getRow($sql);
	if ($result['goods']){
		$temp = json_decode($result['goods'],true);
// 		print_r($temp);
		if (!empty($temp)){
			foreach ($temp as $k=>$v){
				$tempA = explode('|', $v);
				$ids[] = $tempA[1];
			}
			$where = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
					"g.is_delete = 0 AND g.goods_id IN(".implode(',', $ids).")";
			$sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.is_new, g.is_best, g.is_hot, g.shop_price AS org_price, ' .
					"IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, g.promote_price, g.goods_type, " .
					'g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb , g.goods_img, g.collection_id, g.sales_volume, g.goods_number,g.sales_count ' .
					'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
					'LEFT JOIN ' . $GLOBALS['ecs']->table('member_price') . ' AS mp ' .
					"ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' " .
					"WHERE $where GROUP BY g.goods_name ORDER BY g.goods_id ";
			
			$goods = $GLOBALS['db']->getAll($sql);
			$arr = array();
			
			foreach ($goods as $k=>$row){
					
				if ($row['promote_price'] > 0)
				{
					$promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
				}
				else
				{
					$promote_price = 0;
				}
					
				if($GLOBALS['display'] == 'grid')
				{
					$arr[$row['goods_id']]['goods_name']       = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
				}
				else
				{
					$arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
				}
				
				/* 处理商品水印图片 */
				$watermark_img = '';
				
				if ($promote_price != 0)
				{
					$watermark_img = "watermark_promote_small";
				}
				elseif ($row['is_new'] != 0)
				{
					$watermark_img = "watermark_new_small";
				}
				elseif ($row['is_best'] != 0)
				{
					$watermark_img = "watermark_best_small";
				}
				elseif ($row['is_hot'] != 0)
				{
					$watermark_img = 'watermark_hot_small';
				}
				
				if ($watermark_img != '')
				{
					$arr[$row['goods_id']]['watermark_img'] =  $watermark_img;
				}
					
				$arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
				$arr[$row['goods_id']]['promote_price']    = ($promote_price > 0) ? $promote_price : '';
				$arr[$row['goods_id']]['is_new']  =  $row['is_new']  ? $row['is_new']  : 0 ;
				$arr[$row['goods_id']]['is_best'] =  $row['is_best'] ? $row['is_best'] : 0 ;
				$arr[$row['goods_id']]['is_hot']  =  $row['is_hot']  ? $row['is_hot']  : 0 ;
				$arr[$row['goods_id']]['name']             = $row['goods_name'];
				$arr[$row['goods_id']]['shop_price']       = price_format($row['shop_price']);
				$arr[$row['goods_id']]['type']             = $row['goods_type'];
				$arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
				$arr[$row['goods_id']]['url']              = build_uri('goods', array('gid'=>$row['goods_id']));
				$arr[$row['goods_id']]['sales_count']	   = $row['sales_count'];
				$arr[$row['goods_id']]['goods_number']     = $row['goods_number'];
				$arr[$row['goods_id']]['market_price']		= price_format($row['market_price']);
			}
			$result['goods_list'] = $arr;
		}
		
	}
	
	return $result;
}

?>